<?php

namespace app\store\controller\server;

use app\store\controller\Controller;
use think\db;

/**
 * 分销商等级管理
 * Class Setting
 * @package app\store\controller\apps\dealer
 */
class Level extends Controller
{
    /**
     * 等级管理
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $list = Db::name("server_level")->select()->toArray();
        return $this->fetch('index', [
            'list' => $list,
        ]);
    }

    /**
     * 等级编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function edit($id)
    {
        // 等级详情
        $level = Db::name("server_level")->where(array("id"=>$id))->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('edit',[
                'level' => $level,
            ]);
        }

        $data = $this->postData('level');
        // 更新记录
        if (Db::name("server_level")->where(array("id"=>$id))->update(['level_name'=>$data['level_name'],'commission'=>$data['commission']])) {
            return $this->renderSuccess('更新成功', url('server.level/index'));
        }
        return $this->renderError('更新失败');
    }

    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        $data = $this->postData('level');

        if (Db::name("server_level")->insert(['level_name'=>$data['level_name'],'commission'=>$data['commission']])) {
            return $this->renderSuccess('添加成功', url('server.level/index'));
        }
        return $this->renderError('添加失败');
    }

    /**
     * 删除等级
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        $level = Db::name("server_level")->where(array("id"=>$id))->find();
        if ($level) {
            Db::name("server_level")->where(array("id"=>$id))->delete();
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('等级不存在或已删除');
        }

    }



}